export const stuEnroll = {
  data () {
    var validateAge = (rules, value, callback) => {
      let reg = /^\d{1,}$/
      if (value === '') {
        callback(new Error('必填'))
      } else if (!reg.test(value)) {
        callback(new Error('仅支持数字'))
      } else {
        callback()
      }
    }
    var phone = (rules, value, callback) => {
      var reg = /^1([3|4|5|6|7|8|9|])\d{9}$/
      if (value === '') {
        callback(new Error('必填'))
      } else if (!reg.test(value)) {
        callback(new Error('手机号格式不正确'))
      } else {
        callback()
      }
    }
    var dot = (rules, value, callback) => {
      let dot = /^([0-9][0-9]*)+(.[0-9]{1,2})?$/// 最多支持两位小数
      if (value !== '' && !dot.test(value)) {
        callback(new Error('仅支持数字且最多两位小数'))
      } else if (value > 9999) {
        callback(new Error('最大9999分'))
      } else {
        callback()
      }
    }
    var email = (rules, value, callback) => {
      var reg = /^[a-zA-Z][a-zA-Z0-9-]*$/
      if (value === '') {
        callback(new Error('必填'))
      } else if (!reg.test(value)) {
        callback(new Error('格式不正确'))
      } else {
        callback()
      }
    }
    var workYears1 = (rules, value, callback) => {
      let reg = /^\d{1,}$/
      if (!value) {
        callback(new Error('必填'))
      } else if (!reg.test(value)) {
        callback(new Error('仅支持数字'))
      } else {
        callback()
      }
    }
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      setItems: ['formMsg', 'school', 'major', 'ClassType', 'referee', 'wish'],
      must: true,
      formMsg: {
        isEom: false,
        userName: '',
        sex: '',
        age: '',
        userPhone: '',
        userPhoneStatus: false,
        project: '',
        school: '',
        major: '',
        ClassType: '',
        classIdTwo: '',
        classIdThree: '',
        wish: '',
        regPhone: 0,
        appUserId: '',
        refereeName: '',
        refereeDepartmentName: '',
        refereeEmail: '',
        refereeTel: '',
        educations_01: {
          stage: 1,
          record: '',
          schoolId: '',
          majorId: '',
          studyAge: '',
          schoolType: '',
          eduInfoAll: '',
          studyTpye: '',
          isSearch: '',
          majorPoper: 1,
          subects: '',
          cjMinue: '',
          picture_1Num: [],
          picture_2Num: [],
          picture_3Num: []
        },
        isEducations_T: 1,
        educations_02: {
          stage: 2,
          record: '',
          schoolId: '',
          majorId: '',
          studyAge: '',
          schoolType: '',
          eduInfoAll: '',
          studyTpye: '',
          isSearch: '',
          majorPoper: 1,
          subects: '',
          cjMinue: '',
          picture_1Num: [],
          picture_2Num: [],
          picture_3Num: []
        },
        isEducations_third: 1,
        educations_03: {
          stage: 3,
          record: '',
          schoolId: '',
          majorId: '',
          studyAge: '',
          schoolType: '',
          eduInfoAll: '',
          studyTpye: '',
          isSearch: '',
          majorPoper: 1,
          subects: '',
          cjMinue: '',
          picture_1Num: [],
          picture_2Num: [],
          picture_3Num: []
        },
        workMsg_01: {
          isJob: '',
          gsName: '',
          timer: [],
          money: '',
          job: '',
          doing: ''
        },
        work_02: 1,
        workMsg_02: {
          isJob: '2',
          gsName: '',
          timer: [],
          money: '',
          job: '',
          doing: ''
        },
        workYear: '',
        level: '',
        words: '',
        speakLevel: '',
        hearLevel: '',
        readLevel: '',
        writeLevel: '',
        note: '',
        idAdd: false,
        applyTable: [],
        chsiCnEdu: [],
        chsiEnEdu: [],
        pasport: [],
        idCard: [],
        identificationPic: [],
        workProve: [],
        vitae: [],
        degreeIdent: [],
        recommendation: [],
        enVideo: [],
        statement: [],
        scoreCard: []
      },
      rules: {
        userName: [
          { required: true, message: '必填', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '必选', trigger: 'change' }
        ],
        age: [
          { validator: validateAge, required: true, trigger: 'blur' }
        ],
        userPhone: [
          { validator: phone, required: true, trigger: 'blur' }
        ],
        project: [
          { required: true, message: '必选', trigger: 'change' }
        ],
        school: [
          { required: true, message: '必选', trigger: 'change' }
        ],
        major: [
          { required: true, message: '必选', trigger: 'change' }
        ],
        ClassType: [
          { required: true, message: '必选', trigger: 'change' }
        ],
        classIdTwo: [
          { required: true, message: '必选', trigger: 'change' }
        ],
        classIdThree: [
          { required: true, message: '必选', trigger: 'change' }
        ],
        refereeName: [
          { required: true, message: '必填', trigger: 'blur' }
        ],
        refereeDepartmentName: [
          { required: true, message: '必选', trigger: 'change' }
        ],
        refereeEmail: [
          { validator: email, required: true, trigger: 'blur' }
        ],
        refereeTel: [
          { validator: phone, required: true, trigger: 'blur' }
        ],
        'educations_01.cjMinue': [
          { validator: dot, trigger: 'blur' }
        ],
        'educations_02.cjMinue': [
          { validator: dot, trigger: 'blur' }
        ],
        'educations_03.cjMinue': [
          { validator: dot, trigger: 'blur' }
        ]
      },
      addRules: {
        'educations_01.record': [
          { required: true, message: '必选', trigger: 'change' }
        ],
        'educations_01.schoolId': [
          { required: true, message: '必选', trigger: 'blur' }
        ],
        'educations_01.majorId': [
          { required: true, message: '必选', trigger: 'blur' }
        ],
        'educations_01.schoolType': [
          { required: true, message: '必选', trigger: 'change' }
        ],
        'educations_01.eduInfoAll': [
          { required: true, message: '必选', trigger: 'change' }
        ],
        'educations_01.studyTpye': [
          { required: true, message: '必选', trigger: 'change' }
        ],
        'educations_01.studyAge': [
          { required: true, message: '必选', trigger: 'change' }
        ],
        'educations_01.isSearch': [
          { required: true, message: '必选', trigger: 'change' }
        ],
        'educations_01.majorPoper': [
          { required: true, message: '必选', trigger: 'change' }
        ],
        'workMsg_01.isJob': [
          { required: true, message: '必选', trigger: 'change' }
        ],
        'workMsg_01.gsName': [
          { required: true, message: '必填', trigger: 'blur' }
        ],
        'workMsg_01.timer': [
          { required: true, message: '必选', trigger: 'change' }
        ],
        'workMsg_01.money': [
          { required: true, message: '必选', trigger: 'change' }
        ],
        'workMsg_01.job': [
          { required: true, message: '必填', trigger: 'blur' }
        ],
        'workMsg_01.doing': [
          { required: true, message: '必填', trigger: 'blur' }
        ],
        workYear: [
          { validator: workYears1, required: true, trigger: 'blur' }
        ]
      },
      record: [{ // 学历列表
        value: '1',
        label: '大专'
      }, {
        value: '2',
        label: '高中起点专科'
      }, {
        value: '3',
        label: '专升本'
      }, {
        value: '4',
        label: '本科'
      }, {
        value: '5',
        label: '本科以上'
      }],
      studyAge: [ {// 学习年限
        value: '1',
        label: '2年'
      }, {
        value: '2',
        label: '2.5年'
      }, {
        value: '3',
        label: '3年'
      }, {
        value: '4',
        label: '4年'
      }, {
        value: '5',
        label: '5年'
      }, {
        value: '6',
        label: '5年以上'
      }],
      schoolType: [{ // 院校类型
        value: '1',
        label: '二本'
      }, {
        value: '2',
        label: '一本'
      }, {
        value: '3',
        label: '211'
      }, {
        value: '4',
        label: '985'
      }, {
        value: '5',
        label: '其他'
      }],
      englishLevel: [{ // 等级水平
        value: '1',
        label: '零基础'
      }, {
        value: '2',
        label: '初中水平'
      }, {
        value: '3',
        label: '高中水平'
      }, {
        value: '4',
        label: '大学水平无通过任何等级证书'
      }, {
        value: '5',
        label: '大学英语4级'
      }, {
        value: '6',
        label: '大学英语6级'
      }, {
        value: '7',
        label: '英语专业4级'
      }, {
        value: '8',
        label: '英语专业8级'
      }, {
        value: '9',
        label: '雅思 6分以上'
      }, {
        value: '10',
        label: '托福 75分以上'
      }],
      letterNumber: [{ // 词汇量
        value: '1',
        label: '1000以下'
      }, {
        value: '2',
        label: '1000-2000'
      }, {
        value: '3',
        label: '3000-4000'
      }, {
        value: '4',
        label: '4000-6000'
      }, {
        value: '5',
        label: '6000-8000'
      }, {
        value: '6',
        label: '8000以上'
      }],
      money: [{ // 年薪范围
        value: '1',
        label: '5万-10万'
      }, {
        value: '2',
        label: '10万-20万'
      }, {
        value: '3',
        label: '20万-30万'
      }, {
        value: '4',
        label: '30万-50万'
      }, {
        value: '5',
        label: '50万-100万'
      }, {
        value: '6',
        label: '100万以上'
      }],
      resetForm: {
        educations_01: {
          stage: 1,
          record: '',
          schoolId: '',
          majorId: '',
          studyAge: '',
          schoolType: '',
          eduInfoAll: '',
          studyTpye: '',
          isSearch: '',
          majorPoper: 1,
          subects: '',
          cjMinue: '',
          picture_1Num: [],
          picture_2Num: [],
          picture_3Num: []
        },
        isEducations_T: 1,
        educations_02: {
          stage: 2,
          record: '',
          schoolId: '',
          majorId: '',
          studyAge: '',
          schoolType: '',
          eduInfoAll: '',
          studyTpye: '',
          isSearch: '',
          majorPoper: 1,
          subects: '',
          cjMinue: '',
          picture_1Num: [],
          picture_2Num: [],
          picture_3Num: []
        },
        isEducations_third: 1,
        educations_03: {
          stage: 3,
          record: '',
          schoolId: '',
          majorId: '',
          studyAge: '',
          schoolType: '',
          eduInfoAll: '',
          studyTpye: '',
          isSearch: '',
          majorPoper: 1,
          subects: '',
          cjMinue: '',
          picture_1Num: [],
          picture_2Num: [],
          picture_3Num: []
        },
        workMsg_01: {
          isJob: '',
          gsName: '',
          timer: [],
          money: '',
          job: '',
          doing: ''
        },
        work_02: 1,
        workMsg_02: {
          isJob: '2',
          gsName: '',
          timer: [],
          money: '',
          job: '',
          doing: ''
        },
        workYear: '',
        level: '',
        words: '',
        speakLevel: '',
        hearLevel: '',
        readLevel: '',
        writeLevel: '',
        note: '',
        idAdd: false,
        applyTable: [],
        chsiCnEdu: [],
        chsiEnEdu: [],
        pasport: [],
        idCard: [],
        identificationPic: [],
        workProve: [],
        vitae: [],
        degreeIdent: [],
        recommendation: [],
        enVideo: [],
        statement: [],
        scoreCard: []
      }
    }
  },
  methods: {
    // 图片、视频上传/删除回调函数
    upLoadSuccess01 (response, file, fileList) {
      this.formMsg.educations_01.picture_1Num = this.listFile(fileList, 'up')
    },
    upLoadRemove01 (file, fileList) {
      this.formMsg.educations_01.picture_1Num = this.listFile(fileList, 'remove')
    },
    upLoadSuccess02 (response, file, fileList) {
      this.formMsg.educations_01.picture_2Num = this.listFile(fileList, 'up')
    },
    upLoadRemove02 (file, fileList) {
      this.formMsg.educations_01.picture_2Num = this.listFile(fileList, 'remove')
    },
    upLoadSuccess03 (response, file, fileList) {
      this.formMsg.educations_01.picture_3Num = this.listFile(fileList, 'up')
    },
    upLoadRemove03 (file, fileList) {
      this.formMsg.educations_01.picture_3Num = this.listFile(fileList, 'remove')
    },
    upLoadSuccess11 (response, file, fileList) {
      this.formMsg.educations_02.picture_1Num = this.listFile(fileList, 'up')
    },
    upLoadRemove11 (file, fileList) {
      this.formMsg.educations_02.picture_1Num = this.listFile(fileList, 'remove')
    },
    upLoadSuccess12 (response, file, fileList) {
      this.formMsg.educations_02.picture_2Num = this.listFile(fileList, 'up')
    },
    upLoadRemove12 (file, fileList) {
      this.formMsg.educations_02.picture_2Num = this.listFile(fileList, 'remove')
    },
    upLoadSuccess13 (response, file, fileList) {
      this.formMsg.educations_02.picture_3Num = this.listFile(fileList, 'up')
    },
    upLoadRemove13 (file, fileList) {
      this.formMsg.educations_02.picture_3Num = this.listFile(fileList, 'remove')
    },
    upLoadSuccess21 (response, file, fileList) {
      this.formMsg.educations_03.picture_1Num = this.listFile(fileList, 'up')
    },
    upLoadRemove21 (file, fileList) {
      this.formMsg.educations_03.picture_1Num = this.listFile(fileList, 'remove')
    },
    upLoadSuccess22 (response, file, fileList) {
      this.formMsg.educations_03.picture_2Num = this.listFile(fileList, 'up')
    },
    upLoadRemove22 (file, fileList) {
      this.formMsg.educations_03.picture_2Num = this.listFile(fileList, 'remove')
    },
    upLoadSuccess23 (response, file, fileList) {
      this.formMsg.educations_03.picture_3Num = this.listFile(fileList, 'up')
    },
    upLoadRemove23 (file, fileList) {
      this.formMsg.educations_03.picture_3Num = this.listFile(fileList, 'remove')
    },
    applyTableSuccess (response, file, fileList) {
      this.formMsg.applyTable = this.listFile(fileList, 'up')
    },
    applyTableRemove (file, fileList) {
      this.formMsg.applyTable = this.listFile(fileList, 'remove')
    },
    chsiCnEduSuccess (response, file, fileList) {
      this.formMsg.chsiCnEdu = this.listFile(fileList, 'up')
    },
    chsiCnEduRemove (file, fileList) {
      this.formMsg.chsiCnEdu = this.listFile(fileList, 'remove')
    },
    chsiEnEduSuccess (response, file, fileList) {
      this.formMsg.chsiEnEdu = this.listFile(fileList, 'up')
    },
    chsiEnEduRemove (file, fileList) {
      this.formMsg.chsiEnEdu = this.listFile(fileList, 'remove')
    },
    pasportSuccess (response, file, fileList) {
      this.formMsg.pasport = this.listFile(fileList, 'up')
    },
    pasportRemove (file, fileList) {
      this.formMsg.pasport = this.listFile(fileList, 'remove')
    },
    idCardSuccess (response, file, fileList) {
      this.formMsg.idCard = this.listFile(fileList, 'up')
    },
    idCardRemove (file, fileList) {
      this.formMsg.idCard = this.listFile(fileList, 'remove')
    },
    identificationPicSuccess (response, file, fileList) {
      this.formMsg.identificationPic = this.listFile(fileList, 'up')
    },
    identificationPicRemove (file, fileList) {
      this.formMsg.identificationPic = this.listFile(fileList, 'remove')
    },
    workProveSuccess (response, file, fileList) {
      this.formMsg.workProve = this.listFile(fileList, 'up')
    },
    workProveRemove (file, fileList) {
      this.formMsg.workProve = this.listFile(fileList, 'remove')
    },
    vitaeSuccess (response, file, fileList) {
      this.formMsg.vitae = this.listFile(fileList, 'up')
    },
    vitaeRemove (file, fileList) {
      this.formMsg.vitae = this.listFile(fileList, 'remove')
    },
    degreeIdentSuccess (response, file, fileList) {
      this.formMsg.degreeIdent = this.listFile(fileList, 'up')
    },
    degreeIdentRemove (file, fileList) {
      this.formMsg.degreeIdent = this.listFile(fileList, 'remove')
    },
    recommendationSuccess (response, file, fileList) {
      this.formMsg.recommendation = this.listFile(fileList, 'up')
    },
    recommendationRemove (file, fileList) {
      this.formMsg.recommendation = this.listFile(fileList, 'remove')
    },
    enVideoSuccess (response, file, fileList) {
      this.formMsg.enVideo = this.listFile(fileList, 'up')
    },
    enVideoRemove (file, fileList) {
      this.formMsg.enVideo = this.listFile(fileList, 'remove')
    },
    statementSuccess (response, file, fileList) {
      this.formMsg.statement = this.listFile(fileList, 'up')
    },
    statementRemove (file, fileList) {
      this.formMsg.statement = this.listFile(fileList, 'remove')
    },
    scoreCardSuccess (response, file, fileList) {
      this.formMsg.scoreCard = this.listFile(fileList, 'up')
    },
    scoreCardRemove (file, fileList) {
      this.formMsg.scoreCard = this.listFile(fileList, 'remove')
    },
    beforeUpload (file) {
      console.log(file)
      if (file.size > 5 * 1024 * 1024) {
        this.$message({ type: 'error', message: '请上传不超过5M的文件!' })
        return false
      }
    },
    // 过滤上传，删除图片返回值过滤错误信息
    listFile (list, type) {
      return list.reduce((arr, element) => {
        if (!(element.response.data && element.response.data.url) || element.response === 'token error !') {
          if (type === 'up') {
            this.$message.error('上传失败')
          } else {
            this.$message.error('删除失败')
          }
          return arr
        }
        arr.push(element)
        return arr
      }, [])
    },
    submit () {
      this.setItems.forEach((element) => {
        sessionStorage.setItem(element, JSON.stringify(this[element]))
      })
      // 教育经历
      let eduInfo = []
      if (this.formMsg.isEducations_T === 1 && this.formMsg.isEducations_third === 1) {
        eduInfo = [this.formMsg.educations_01]
      } else if (this.formMsg.isEducations_T === 1) {
        eduInfo = [this.formMsg.educations_01, this.formMsg.educations_03]
      } else if (this.formMsg.isEducations_third === 1) {
        eduInfo = [this.formMsg.educations_01, this.formMsg.educations_02]
      } else {
        eduInfo = [this.formMsg.educations_01, this.formMsg.educations_02, this.formMsg.educations_03]
      }
      let eduExperienceList = eduInfo.reduce((arr, element) => {
        var graduateCertPic = element.picture_1Num.reduce((arr, element) => {
          arr.push(element.response.data.url)
          return arr
        }, [])
        let eduCertPic = []
        if (element.majorPoper === 2) {
          eduCertPic = element.picture_2Num.reduce((arr, element) => {
            arr.push(element.response.data.url)
            return arr
          }, [])
        } else {
          eduCertPic = []
        }
        var gradeFormPic = element.picture_3Num.reduce((arr, element) => {
          arr.push(element.response.data.url)
          return arr
        }, [])
        arr.push({
          'stage': element.stage,
          'level': element.record,
          'schoolName': element.schoolId,
          'professionName': element.majorId,
          'eduYear': element.studyAge,
          'schoolType': element.schoolType,
          'isFullTime': element.eduInfoAll,
          'learnModal': element.studyTpye,
          'isCheck': element.isSearch,
          'isEducationCert': element.majorPoper,
          'subjectNumOfGrades': element.subects,
          'averageGrades': element.cjMinue,
          graduateCertPic,
          eduCertPic,
          gradeFormPic
        })
        return arr
      }, [])
      // 工作经历
      let workInfo = []
      if (this.formMsg.work_02 === 0) {
        workInfo = [this.formMsg.workMsg_01, this.formMsg.workMsg_02]
      } else {
        workInfo = [this.formMsg.workMsg_01]
      }
      let workExperienceList = workInfo.reduce((arr, element) => {
        arr.push({
          'isOnJob': element.isJob,
          'companyName': element.gsName,
          'onJobDateFrom': element.timer[0],
          'onJobDateTo': element.timer[1],
          'salary': element.money,
          'job': element.job,
          'workDirection': element.doing
        })
        return arr
      }, [])
      // 解构
      let { isEom, userName, sex, age, userPhone, regPhone, workYear, appUserId, refereeName, refereeEmail, refereeTel, project,
        classIdTwo, classIdThree, wish, school, major, ClassType, level, words, speakLevel,
        hearLevel, readLevel, writeLevel, idAdd, refereeDepartmentName, applyTable, chsiCnEdu, chsiEnEdu, pasport, idCard,
        identificationPic, note, workProve, vitae, degreeIdent, recommendation, enVideo, statement, scoreCard } = this.formMsg
      let projectLsit = this.project.find(item => project === item.id)
      let schoolName = this.school.find(item => school === item.schoolId)
      let degreeList = schoolName.degreeList.find(item => major === item.degreeId)
      let classList = degreeList.classList.find(item => ClassType === item.classId)
      let classIdTwoList = {}
      let classIdThreeList = {}
      let wishCopy = {}
      if (project === 7) {
        classIdTwoList = degreeList.classList.find(item => classIdTwo === item.classId)
        classIdThreeList = degreeList.classList.find(item => classIdThree === item.classId)
      }
      if (wish) {
        wishCopy = this.wish.find(item => item.id === wish)
      }
      let departmentList = this.referee.find(item => refereeDepartmentName === item.departmentId)
      let allInfo = {
        stuEnrollInfo: {
          isEom,
          name: userName, // 学员姓名
          sex,
          age,
          mobile: userPhone, // 手机号
          regPhone,
          projectName: projectLsit.name, // 申请项目
          projectId: projectLsit.id, // 申请项目ID
          schoolName: schoolName.schoolName, // 意向学院
          schoolId: schoolName.schoolId, // 意向学院ID
          degreeName: degreeList.degreeName, // 学位
          degreeId: degreeList.degreeId, // 学位ID
          className: classList.className, // 班型
          classId: classList.classId, // 班型ID
          classIdTwoName: classIdTwoList.className,
          classIdTwo: classIdTwoList.classId,
          classIdThreeName: classIdThreeList.className,
          classIdThree: classIdThreeList.classId,
          wish,
          wishMsg: wish ? wishCopy.academyName : '',
          workYear, // 迄今为止工作年限
          appUserId,
          refereeName, // 咨询师姓名
          refereeDepartmentName: departmentList.departmentName, // 咨询师事业部
          refereeDepartmentId: departmentList.departmentId, // 咨询师事业部
          refereeEmail, // 咨询师邮箱
          refereeTel, // 咨询师手机号
          note, // 备注
          source: 0 // 来源
        },
        englishExperience: { // 英语能力信息
          level,
          words,
          speakLevel,
          hearLevel,
          readLevel,
          writeLevel
        },
        eduExperienceList, // 教育信息
        workExperienceList, // 工作信息
        idAdd,
        additionalStuff: {},
        schoolCategoryId: project,
        schoolId: schoolName.schoolId,
        degreeId: degreeList.degreeId,
        classId: classList.classId
      }
      // 补充条件
      let addList = [applyTable, chsiCnEdu, chsiEnEdu, pasport, idCard, identificationPic, workProve, vitae, degreeIdent, recommendation, enVideo, statement, scoreCard]
      let addNames = ['applyTable', 'chsiCnEdu', 'chsiEnEdu', 'pasport', 'idCard', 'identificationPic', 'workProve', 'vitae', 'degreeIdent', 'recommendation', 'enVideo', 'statement', 'scoreCard']
      addList.forEach((element, index) => {
        allInfo.additionalStuff[addNames[index]] = element.reduce((arr, ele) => {
          arr.push(ele.response.data.url)
          return arr
        }, [])
      })
      return allInfo
    }
  },
  created () {
    this.setItems.forEach((element) => {
      if (!JSON.parse(sessionStorage.getItem(element))) return
      this[element] = JSON.parse(sessionStorage.getItem(element))
    })
  }
}
